/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Practica3;

import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.swing.JOptionPane;

/**
 *
 * @author RUDY
 */
public class frm_EsquemaRelacion_GestorPaginas extends javax.swing.JFrame {
    
    String nombreArchivo = "D:/BD2_Practica03/Tupla.dat";
    DefaultTableModel table_model_scheme=new DefaultTableModel();
    DefaultTableModel table_model_tupla=new DefaultTableModel();
    DefaultTableModel table_model_types=new DefaultTableModel();
    DefaultListModel list_result=new DefaultListModel();
    public Object[] columns_scheme={"Nombre","Tipo","Tamaño"};  
    
    List<Scheme> schemes=new ArrayList<Scheme>();
    String scheme_name="";
    int scheme_actual=-1;
       
    List<GestorIOTupla> listas = new ArrayList<GestorIOTupla>();
    List<Tupla> Tupla = new ArrayList<Tupla>();
           
    /**
     * Creates new form frm_EsquemaRelacion_GestorPaginas
     */
    public frm_EsquemaRelacion_GestorPaginas() {
        initComponents();
        this.setLocationRelativeTo(null); 
        ClearTableScheme();  
        lst_result_add_tupla.setModel(list_result);
    }    
    public static final String[] DATA = {"String","Integer","Char","Byte","Short","Long","Float","Double"}; 

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbl_scheme = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txt_scheme_name = new javax.swing.JTextField();
        txt_scheme_size = new javax.swing.JTextField();
        btn_create_scheme = new javax.swing.JButton();
        btn_save_scheme = new javax.swing.JButton();
        btn_new_scheme = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbl_tupla = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        cb_scheme = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        txt_num_tuplas = new javax.swing.JTextField();
        btn_create_add_tupla = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        lst_result_add_tupla = new javax.swing.JList();
        btn_tupla_ok = new javax.swing.JButton();
        btn_tupla_save = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tbl_tipo_atributo = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Gestion de Tuplas                                                                                                                                                                                                        Rudy");

        tbl_scheme.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tbl_scheme);

        jLabel1.setText("Nombre del Esquema :");

        jLabel2.setText("Nro de atributos del Esquema:");

        btn_create_scheme.setText("Crear");
        btn_create_scheme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_create_schemeActionPerformed(evt);
            }
        });

        btn_save_scheme.setText("Guardar");
        btn_save_scheme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_save_schemeActionPerformed(evt);
            }
        });

        btn_new_scheme.setText("Nuevo");
        btn_new_scheme.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_new_schemeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 870, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(btn_save_scheme, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btn_new_scheme, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txt_scheme_size))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                    .addComponent(jLabel1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(txt_scheme_name, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(btn_create_scheme, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txt_scheme_name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txt_scheme_size, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btn_create_scheme)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 346, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_save_scheme)
                    .addComponent(btn_new_scheme))
                .addContainerGap())
        );

        jTabbedPane1.addTab("Crear Esquema", jPanel1);

        tbl_tupla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tbl_tupla);

        jLabel3.setText("Seleccione el Esquema :");

        jLabel4.setText("Nro de tuplas :");

        btn_create_add_tupla.setText("Crear");
        btn_create_add_tupla.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_create_add_tuplaActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(lst_result_add_tupla);

        btn_tupla_ok.setText("Aceptar");
        btn_tupla_ok.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_tupla_okActionPerformed(evt);
            }
        });

        btn_tupla_save.setText("Guardar en Disco");
        btn_tupla_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_tupla_saveActionPerformed(evt);
            }
        });

        jLabel5.setText("D:/BD2_Practica03/tupla.bat");

        tbl_tipo_atributo.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane4.setViewportView(tbl_tipo_atributo);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2)
                    .addComponent(jScrollPane3)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 870, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(cb_scheme, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(txt_num_tuplas, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(47, 47, 47)
                                .addComponent(btn_create_add_tupla, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(btn_tupla_ok, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(49, 49, 49)
                                .addComponent(btn_tupla_save, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel5)))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cb_scheme, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txt_num_tuplas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn_create_add_tupla))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_tupla_ok)
                    .addComponent(btn_tupla_save)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("Agregar Tuplas", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_create_schemeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_create_schemeActionPerformed
        // TODO add your handling code here:
        ClearTableScheme();
        if(!txt_scheme_name.getText().equals("")&!txt_scheme_size.getText().equals(""))
        {
            scheme_name=txt_scheme_name.getText();
            table_model_scheme.setNumRows(Integer.parseInt(txt_scheme_size.getText()));
            for (int i = 0; i < Integer.parseInt(txt_scheme_size.getText()); i++) {
                   table_model_scheme.setValueAt("String", i, 1);
                } 
        }
        else
        {
            JOptionPane.showMessageDialog(frm_EsquemaRelacion_GestorPaginas.this,"Complete todos los campos.", "ATENCION", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btn_create_schemeActionPerformed

    private void btn_save_schemeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_save_schemeActionPerformed
        // TODO add your handling code here:
        boolean ok=true;
        try {
            List<Atributo> attrs = new ArrayList<Atributo>();
            for (int i = 0; i < table_model_scheme.getRowCount(); i++) {
                if(tbl_scheme.getValueAt(i, 1).equals("String"))
                {
                    attrs.add(new Atributo(tbl_scheme.getValueAt(i, 0).toString(), String.class));
                }
                if(tbl_scheme.getValueAt(i, 1).equals("Integer"))
                {
                    attrs.add(new Atributo(tbl_scheme.getValueAt(i, 0).toString(), Integer.class));
                }
                if(tbl_scheme.getValueAt(i, 1).equals("Char"))
                {
                    attrs.add(new Atributo(tbl_scheme.getValueAt(i, 0).toString(), Character.class));
                }
                if(tbl_scheme.getValueAt(i, 1).equals("Byte"))
                {
                    attrs.add(new Atributo(tbl_scheme.getValueAt(i, 0).toString(), Byte.class));
                }
                if(tbl_scheme.getValueAt(i, 1).equals("Short"))
                {
                    attrs.add(new Atributo(tbl_scheme.getValueAt(i, 0).toString(), Short.class));
                }
                if(tbl_scheme.getValueAt(i, 1).equals("Long"))
                {
                    attrs.add(new Atributo(tbl_scheme.getValueAt(i, 0).toString(), Long.class));
                }            
                if(tbl_scheme.getValueAt(i, 1).equals("Float"))
                {
                    attrs.add(new Atributo(tbl_scheme.getValueAt(i, 0).toString(), Float.class));
                }
                if(tbl_scheme.getValueAt(i, 1).equals("Double"))
                {
                    attrs.add(new Atributo(tbl_scheme.getValueAt(i, 0).toString(), Double.class));
                }
            }       
            Relacion rel = new Relacion(attrs);
            Scheme esquema=new Scheme(scheme_name, attrs, rel);
            schemes.add(esquema);
            cb_scheme.removeAllItems();
            for (int i = 0; i <schemes.size(); i++) {
                cb_scheme.addItem(schemes.get(i).getScheme_name());
            }
        } catch (Exception e) {
            ok=false;
        }
        if(ok)
        {
            JOptionPane.showMessageDialog(frm_EsquemaRelacion_GestorPaginas.this,"Guardado.", "ATENCION", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btn_save_schemeActionPerformed

    private void btn_new_schemeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_new_schemeActionPerformed
        // TODO add your handling code here:
        txt_scheme_name.setText("");
        txt_scheme_size.setText("");
        scheme_name="";
        ClearTableScheme();
    }//GEN-LAST:event_btn_new_schemeActionPerformed

    private void btn_create_add_tuplaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_create_add_tuplaActionPerformed
        // TODO add your handling code here:
        int scheme_selecction=cb_scheme.getSelectedIndex();
        scheme_actual=scheme_selecction;
        String scheme_2p=schemes.get(scheme_selecction).getScheme_name();
        int nro_atributos=schemes.get(scheme_selecction).getAttrs().size();
        String[] names=new String[nro_atributos];
        String[] types=new String[nro_atributos];
        for (int i = 0; i < nro_atributos; i++) {            
            names[i]=schemes.get(scheme_selecction).getAttrs().get(i).getNombre();
            types[i]=schemes.get(scheme_selecction).getAttrs().get(i).getTipo().getSimpleName();
        }
        ClearTableTupla(names,types);
        JTextField textField_2 = new JTextField();
        DefaultCellEditor dCE_tf_2=new DefaultCellEditor(textField_2);
        for (int i = 0; i < nro_atributos; i++) {
            tbl_tupla.getColumnModel().getColumn(i).setCellEditor(dCE_tf_2);
        }
        table_model_tupla.setNumRows(Integer.parseInt(txt_num_tuplas.getText()));
    }//GEN-LAST:event_btn_create_add_tuplaActionPerformed

    private void btn_tupla_okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_tupla_okActionPerformed
        // TODO add your handling code here:    
        if(!txt_num_tuplas.getText().equals(""))
        {
            listas = new ArrayList<GestorIOTupla>();
            Tupla = new ArrayList<Tupla>();

            list_result.clear();
            int rows=tbl_tupla.getRowCount();  
            int columns=tbl_tupla.getColumnCount();  
            boolean error=false;
            for (int i = 0; i < rows; i++) {  
                List<Comparable> List_Tuplas = new ArrayList<Comparable>();
                for (int j = 0; j < columns; j++) {
                   String typename=schemes.get(scheme_actual).getAttrs().get(j).getTipo().getSimpleName();
                   if(typename.equals("Char"))
                   {
                            try {
                                 if(!tbl_tupla.getValueAt(i, j).equals(""))//char
                                 {List_Tuplas.add(new Character(tbl_tupla.getValueAt(i, j).toString().toCharArray()[0]));}else
                                 {list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n");error=true;}
                             } catch (Exception e) {
                                 list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". "+e);error=true;
                             }  
                   }
                   if(typename.equals("Byte"))
                   {
                             try {
                                 if(!tbl_tupla.getValueAt(i, j).equals(""))//byte
                                 {List_Tuplas.add(new Byte(tbl_tupla.getValueAt(i, j).toString()));}else
                                 {list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n");error=true;}
                             } catch (Exception e) {
                                 list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n"+e);error=true;
                             }
                   }
                   if(typename.equals("Short"))
                   {
                             try {
                                 if(!tbl_tupla.getValueAt(i, j).equals(""))//short
                                 {List_Tuplas.add(new Short(tbl_tupla.getValueAt(i, j).toString()));}else
                                 {list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n");error=true;}
                             } catch (Exception e) {
                                 list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n"+e);error=true;
                             }
                   }
                   if(typename.equals("Integer"))
                   {
                             try {
                                 if(!tbl_tupla.getValueAt(i, j).equals(""))//integer
                                 {List_Tuplas.add(new Integer(tbl_tupla.getValueAt(i, j).toString()));}else
                                 {list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n");error=true;}
                             } catch (Exception e) {
                                 list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n"+e);error=true;
                             }
                   }
                   if(typename.equals("Long"))
                   {              
                             try {
                                 if(!tbl_tupla.getValueAt(i, j).equals(""))//long
                                 {List_Tuplas.add(new Long(tbl_tupla.getValueAt(i, j).toString()));}else
                                 {list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n");error=true;}
                             } catch (Exception e) {
                                 list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n"+e);error=true;
                             }
                    }
                   if(typename.equals("Float"))
                   {
                             try {
                                  if(!tbl_tupla.getValueAt(i, j).equals(""))//float
                                 {List_Tuplas.add(new Float(tbl_tupla.getValueAt(i, j).toString()));}else
                                 {list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n");error=true;}
                             } catch (Exception e) {
                                 list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n"+e);error=true;
                             }
                    }
                   if(typename.equals("Double"))
                   {
                             try {
                                 if(!tbl_tupla.getValueAt(i, j).equals(""))//double
                                 {List_Tuplas.add(new Double(tbl_tupla.getValueAt(i, j).toString()));}else
                                 {list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n");error=true;}
                             } catch (Exception e) {
                                 list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n"+e);error=true;
                             }
                    }
                   if(typename.equals("String"))
                   {
                             try {
                                 if(!tbl_tupla.getValueAt(i, j).equals(""))//string
                                 {List_Tuplas.add(new String(tbl_tupla.getValueAt(i, j).toString()));}else
                                 {list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n");error=true;}
                             } catch (Exception e) {
                                 list_result.addElement("Error: row "+(i+1)+" column "+(j+1)+". \n"+e);error=true;
                             }               
                    }
                }            
                if(!error)
                {

                    Tupla t = new Tupla(new IdentificadorTupla(schemes.get(scheme_actual).getScheme_name(), i), List_Tuplas);
                    GestorIOTupla gestor = new GestorIOTupla(schemes.get(scheme_actual).getRelation(),schemes.get(scheme_actual).getScheme_name());

                    Tupla.add(t);
                    listas.add(gestor);
                }
            }
            if(!error)
            {
                int inicio =0;
                for (int j = 0; j < rows; j++)
                {
                    Tupla t = Tupla.get(j);
                    list_result.addElement("------------------------------- "+j+" ----------------------------------");
                    list_result.addElement("Escribiendo la Tupla..." + t);
                    byte[] bytes = new byte[1024];
                    GestorIOTupla a  = listas.get(j);
                    a.escribirTupla(t, bytes, inicio);

                    list_result.addElement("Mostrando el arreglo de bytes: "+bytes);

                    list_result.addElement("Leendo la Tupla...");            
                    Par<Tupla, Integer> st = a.leerTupla(bytes, inicio);
                    list_result.addElement(st);
                    inicio+=st.segundo;
                    list_result.addElement("---------------------------------------------------------------------");
                    list_result.addElement(" ");
                }
            }
            else
            {
                list_result.addElement("Error.");
            }        
        }
        else
        {
            JOptionPane.showMessageDialog(frm_EsquemaRelacion_GestorPaginas.this,"Complete todos los campos.", "ATENCION", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btn_tupla_okActionPerformed

    private void btn_tupla_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_tupla_saveActionPerformed
        // TODO add your handling code here:  
        try {                   
            list_result.clear();
            java.io.RandomAccessFile raFile =new java.io.RandomAccessFile(nombreArchivo, "rw");
            for (int i = 0; i < Tupla.size(); i++) {
                IdentificadorPagina idPag =new IdentificadorPagina(nombreArchivo, i);           
                Pagina page = new Pagina(schemes.get(scheme_actual).getRelation(), idPag);
                page.adicionarTupla(Tupla.get(i));
                list_result.addElement(page);
                GestorPagina.escribirPagina(raFile, page);
            }              
            raFile.close();
            
            raFile = new java.io.RandomAccessFile(nombreArchivo, "r");
            for (int i = 0; i < Tupla.size(); i++) {                
                list_result.addElement("Leyendo pagina...");
                Pagina page =GestorPagina.leerPagina(schemes.get(scheme_actual).getRelation(),new IdentificadorPagina(nombreArchivo, i), raFile);
                list_result.addElement(page);
            }     
            raFile.close();
        } catch (Exception e) {
            list_result.addElement("Exception: " + e.getMessage());
            e.printStackTrace(System.err);
        }
             
    }//GEN-LAST:event_btn_tupla_saveActionPerformed

    public void ClearTableScheme()
    {
        table_model_scheme=new DefaultTableModel();
        table_model_scheme.setColumnIdentifiers(columns_scheme); 
        tbl_scheme.setModel(table_model_scheme);
        
        JTextField textField = new JTextField();
        DefaultCellEditor dCE_tf=new DefaultCellEditor(textField);
        tbl_scheme.getColumnModel().getColumn(0).setCellEditor(dCE_tf);
        tbl_scheme.getColumnModel().getColumn(2).setCellEditor(dCE_tf);
        
        JComboBox comboBox = new JComboBox(DATA);
        DefaultCellEditor dCE_cb=new DefaultCellEditor(comboBox);
        tbl_scheme.getColumnModel().getColumn(1).setCellEditor(dCE_cb);
        
        tbl_scheme.setRowHeight(28);   
    }
    public void ClearTableTupla(String[] names,String[] types)
    {
        table_model_types=new DefaultTableModel();
        table_model_types.setColumnIdentifiers(types);
        tbl_tipo_atributo.setModel(table_model_types);
        tbl_tipo_atributo.setRowHeight(19);
        
        table_model_tupla=new DefaultTableModel();
        table_model_tupla.setColumnIdentifiers(names);
        tbl_tupla.setModel(table_model_tupla);
        tbl_tupla.setRowHeight(28);       
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frm_EsquemaRelacion_GestorPaginas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frm_EsquemaRelacion_GestorPaginas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frm_EsquemaRelacion_GestorPaginas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frm_EsquemaRelacion_GestorPaginas.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frm_EsquemaRelacion_GestorPaginas().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_create_add_tupla;
    private javax.swing.JButton btn_create_scheme;
    private javax.swing.JButton btn_new_scheme;
    private javax.swing.JButton btn_save_scheme;
    private javax.swing.JButton btn_tupla_ok;
    private javax.swing.JButton btn_tupla_save;
    private javax.swing.JComboBox cb_scheme;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JList lst_result_add_tupla;
    private javax.swing.JTable tbl_scheme;
    private javax.swing.JTable tbl_tipo_atributo;
    private javax.swing.JTable tbl_tupla;
    private javax.swing.JTextField txt_num_tuplas;
    private javax.swing.JTextField txt_scheme_name;
    private javax.swing.JTextField txt_scheme_size;
    // End of variables declaration//GEN-END:variables
}
